﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KSUNO_EMS.Models
{
    public class EmployeeScheduleDTO
    {
        public int Id { get; set; }
        public string Employee { get; set; }

        public string Monday { get; set; }
        public string Tuesday { get; set; }
        public string Wednesday { get; set; }
        public string Thursday { get; set; }
        public string Friday { get; set; }
        public string Saturday { get; set; }
        public string Sunday { get; set; }

        public class Schedule
        {
            public DateTime From { get; set; }
            public DateTime To { get; set; }
        }
        public static EmployeeScheduleDTO FromDatabase(Employee employee, IEnumerable<EmployeeSchedule> schedule)
        {
            var dto = new EmployeeScheduleDTO() { Id = employee.Id, Employee = employee.FirstName + " " + employee.LastName };
            foreach (var scheduleEntry in schedule.GroupBy(g => g.StartDateTime.DayOfWeek).Select(g =>
                new
                    {
                        DayOfWeek = g.Key,
                        Times = String.Join(", ", g.Select(d => String.Format(@"{0:h\:mmtt}-{1:h\:mmtt}", d.StartDateTime, d.EndDateTime)))
                    }))
                switch (scheduleEntry.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        dto.Monday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Tuesday:
                        dto.Tuesday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Wednesday:
                        dto.Wednesday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Thursday:
                        dto.Thursday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Friday:
                        dto.Friday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Saturday:
                        dto.Saturday = scheduleEntry.Times;
                        break;
                    case DayOfWeek.Sunday:
                        dto.Sunday = scheduleEntry.Times;
                        break;
                }

            return dto;
        }
    }
}